package com.neusoft.hifly.netty.socket.handler;

import org.springframework.kafka.core.KafkaTemplate;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class SocketServerInitialzer extends ChannelInitializer<SocketChannel> {

	// kafka
	private KafkaTemplate<String, Object> kafkaTemplate;

	public SocketServerInitialzer(KafkaTemplate<String, Object> kafkaTemplate) {
		super();
		this.kafkaTemplate = kafkaTemplate;
	}

	@Override
	public void initChannel(SocketChannel channel) {
		// 添加编解码
		channel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
		channel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
		// 自定义通道
		channel.pipeline().addLast("socketHandler", new SocketChatHandler(kafkaTemplate));
	}

}